<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *
 */
class Plugin_Sys extends Plugin
{
	private function _getLatestNew(){
		$ci = & get_instance();
		$latestNews = $ci->db->select("blog.*")->from("blog")
						->join("blog_categories","blog.category_id=blog_categories.id")
						->where( array("blog_categories.slug" => 'latest-news' , "blog.status" => 'live') )
						->order_by('blog.created_on','desc')
						->limit(3)
						->get()->result();
		$html = "<ul>";
		foreach($latestNews as $item){
			$html .= "<li><div class=\"wrap\"><a href='".site_url("blog/view/".$item->slug)."' >".($item->title)."</a>".
					"</h3></div><div class=\"arrow\"></div></li>";
		}
		$html .= "</ul>";	
		return $html;
	}
	
	function _getHomepageText(){
		$rs = $this->db->where('slug','homepage-text')->get('blog')->result();
		if($rs) return "<div class='wrap-text-homepage'>". $rs[0]->body. "</div>";
		return '';
	}
	
	function findUserWithRank($rank){
		$ci = & get_instance();
		$tid = intval($_GET['tid']);
		$rank_array = $ci->user_tournament_entity->getArrayListOfTournament($tid);
		if(count($rank_array) < $rank){return null;}
		if( $rank_array[$rank-1]->point){
			$user_id = $rank_array[$rank-1]->user_id;
			return clone_($ci->user_entity->init('id', $user_id));
		}
		return null;
	}
	
	function getMyPoint($user_id){
		$ci = & get_instance();
		$tid = intval($_GET['tid']);
		$rank_array = $ci->user_tournament_entity->getArrayListOfTournament($tid);
		$p = null;
		foreach($rank_array as $item){
			if($item->user_id == $user_id){
				$p = $item->point;
				break;
			}
		}	
		if($p > 0 ) return $p;
		return null;
	}
	
	private function _tournament_prize($tid){
		$ci = & get_instance();
		$page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0 ;
		$records_per_page = TOP_RANKS_PER_PAGE;
		$rank = $page + 1;
		
		$TOURNAMENT = $ci->tournament_entity->init("id", $tid);
		$numberTopRank = $TOURNAMENT->getNumberTopRank();
		
		if($TOURNAMENT->getShowTournamentRank() != 1 AND $TOURNAMENT->getShowWeeklyRank() != 1){
			return '';
		}
		//$rank_array = $ci->user_tournament_entity->getArrayListOfTournament($tid);
		//$prizeListArray = $ci->prize_entity->getArrayListOfPrize($tid);
		
		if(!isset($_SESSION['sys_prize_type'])){
			$_SESSION['sys_prize_type'] = 'weekly';
		}
		
		if($TOURNAMENT->getShowTournamentRank() != 1){
			$enableTournament = 'hidden';
		}else{
			$enableTournament = '';
		}
		if($TOURNAMENT->getShowWeeklyRank() != 1){
			$enableWeekly = 'hidden';
		}else{
			$enableWeekly = '';
		}
		
		$week_prize_array = $ci->week_prize_entity->getLiveWeekPrizeOfTournament($tid);
		if($week_prize_array){
			$cat_info = $week_prize_array['cat_info'][0];
			$endTime = $cat_info->endtime - time();
			$contest = "<span class=\"text-title $enableWeekly\">Today's Contest Ends in:</span>";
			$contest .= "<div class=\"clrboth\"></div>
						<span class=\"brown countdown upper $enableWeekly\" id=\"countdown_wp_{$cat_info->id}\" rel=\"{$endTime}\"></span>
						";
			if($cat_info->enable == 'no'){
				$_SESSION['sys_prize_type'] = 'tournament';
			}			
		}else{
			$contest = '';
		}
		
		$endTime2 = $TOURNAMENT->getEndTime() - time();
		$contest2 = "<span class=\"text-title $enableTournament\">Tournament Ends in:</span>";
		$contest2 .= "<div class=\"clrboth\"></div>
					<span class=\"brown countdown upper $enableTournament\" id=\"countdown_tct_{$TOURNAMENT->getId()}\" rel=\"{$endTime2}\"></span>
					";
		
		$content = '';
		
		if($_SESSION['sys_prize_type'] != 'weekly'){
			$total = count( $ci->db->where("tournament_id = $tid AND point IS NOT NULL")->get(TBL_USER_TOURNAMENT)->result() );
			$total = $total <= $numberTopRank ? $total : $numberTopRank;
			
			$pagination = create_pagination( 
										$uri = 'cricket/contentload/top_rank/?tid='.$tid, 
										$total_rows = $total , 
										$limit= $records_per_page,
										$uri_segment = 0,
										TRUE, TRUE 
									);
								 
			$records = $this->db->where("tournament_id = $tid AND point IS NOT NULL")->order_by('point','desc')->limit($records_per_page, $page )->get(TBL_USER_TOURNAMENT)->result();
			
			foreach($records as $item){
				$USER = $this->findUserWithRank($rank);
				$PRIZE = getPrizeObject($rank, $tid);
				$thumb = $uname = $points = $rankicon = '';
				if($USER){
					$thumb = thumb($USER->getMyAvatar(),USER_TOP_RANK_W,USER_TOP_RANK_H,true); 
					$uname = $USER->getUserName();
					$points = bet_format( $ci->user_tournament_entity->getMyPointInTournament($USER->getId(),$tid) );
				}
				$worth = '';
				if($PRIZE){
					$worth = "Worth ".$PRIZE->getValue();
				}
				$rankicon = getRankIcon($rank,$tid, "style='float:left;margin-bottom:0px;margin-top:5px;text-align:center;'",60,40);
			 // if($USER AND $PRIZE){ 
					$content .= "
						<div class=\"rowitem2 rank-wrap $enableTournament\">
							<div class=\"grid2 rankshow-wrap\">
								<div class=\"rank-show\">{$rank} </div>
								<div class=\"avatar-show\">
									<div class=\"avatar-wrap\">
										$thumb
										</br>
										$uname
									</div>
								</div>
							</div>
							
							<div class=\"grid1 pointshow\">
								<div class=\"point-wrap\">
									{$points}
								</div>
							</div>
							<div class=\"grid2 rankprize\" >
								<div class=\"icon-wrap\">
									{$rankicon}
								</div>
								<span>{$worth}</span>
							</div>
							<div class=\"clrboth\"></div>
						</div>
						<div class=\"clrboth\"></div>";
						$rank++;
				//}
			}	
		 
		}else{
			if($week_prize_array){
				$allRanksArray = $ci->week_prize_entity->getArrayWeeklyRanksOfTournament($tid,$cat_info->id);
				$total = count($allRanksArray);
				$total = $total <= $numberTopRank ? $total : $numberTopRank;
				$pagination = create_pagination( 
										$uri = 'cricket/contentload/top_rank/?tid='.$tid, 
										$total_rows = $total , 
										$limit= $records_per_page,
										$uri_segment = 0,
										TRUE, TRUE 
									);
				$tmpArr = array_chunk($allRanksArray, $records_per_page, true);
				$index= intval($page/$records_per_page);
				$records = $tmpArr[$index];
			}else{
				$pagination['links'] ='';
				$records = array();
			}
	 
			foreach($records as $item){
				$USER = $ci->week_prize_entity->getUserObjectWithRank($rank,$tid,$cat_info->id);
				$PRIZE = $ci->week_prize_entity->getPrizeObjectWithARank($rank, $cat_info->id);
				$thumb = $uname = $points = $rankicon = '';
				if($USER){
					$userInfo = $ci->week_prize_entity->getPersonWithARank($rank,$tid,$cat_info->id);
					$thumb = thumb($USER->getMyAvatar(),USER_TOP_RANK_W,USER_TOP_RANK_H,true); 
					$uname = $USER->getUserName();
					$points = bet_format( $userInfo['point'] );
				}
				$worth = '';
				$prizeImage = '';
				if($PRIZE){
					$worth = "Worth ".$PRIZE->getValue();
					$prizeImage = $PRIZE->getPrizeImage();
				}
				$ext =  "style='float:left;margin-bottom:0px;margin-top:5px;text-align:center;'";
				if($prizeImage){
					$rankicon = "<img src='".thumb2($prizeImage,PRIZE_TOP_RANK_W,PRIZE_TOP_RANK_H)."' $ext />";
				}
				//if($USER AND $PRIZE){
					$content .= "
						<div class=\"rowitem2 rank-wrap $enableWeekly\">
							<div class=\"grid2 rankshow-wrap\">
								<div class=\"rank-show\">{$rank} </div>
								<div class=\"avatar-show\">
									<div class=\"avatar-wrap\">
										$thumb
										</br>
										$uname
									</div>
								</div>
							</div>
							
							<div class=\"grid1 pointshow\">
								<div class=\"point-wrap\">
									{$points}
								</div>
							</div>
							<div class=\"grid2 rankprize\" >
								<div class=\"icon-wrap\">
									{$rankicon}
								</div>
								<span>{$worth}</span>
							</div>
							<div class=\"clrboth\"></div>
						</div>
						<div class=\"clrboth\"></div>";
						$rank++;
				//}		
			}	
			
		}
		
		$weeklyDIV = $tournamentDIV = '';
		if($_SESSION['sys_prize_type'] == 'weekly'){
			$weeklyDIV = 'act';
			$hideContest = '';
			$hideContest2 = 'hidden';
			$titleClass = 'title'; 
		}else{
			$tournamentDIV = 'act';
			$hideContest = 'hidden';
			$hideContest2 = '';
			$titleClass = 'title2';
		}
		 
		$html = "	
			<div class=\"block\">
				<div id=\"content-tab\">
					<div class=\"weekly-tab {$hideContest} {$enableWeekly}\">
						{$contest}
					</div>
					<div class=\"tournament-tab {$hideContest2} {$enableTournament}\">{$contest2}</div>
				</div>
				<div class=\"clrboth\"></div>
				
				<div id=\"prize-tab\">
					<div id=\"weekly-tab\" class='$weeklyDIV tab $enableWeekly' rel='weekly'><span class=\"text-title\">Today's Ranks</span></div>
					<div id=\"tournament-tab\" class='$tournamentDIV tab $enableTournament' rel='tournament'><span class=\"text-title\">Tournament Ranks</span></div>
				</div>
				<div class=\"clrboth\"></div>
				
				<div class=\"{$titleClass}\" id='rank_title_div'>
					<div class=\"grid2 currentrank\" ><span class=\"left bold\" style=\"margin-left:10px;\">Current Rank</span></div>
					<div class=\"grid1 point\" ><span class=\"left bold\" >Points</span></div>
					<div class=\"grid2 prize\" ><span class=\"bold\" >Prize</span></div>
					<div class=\"clrboth\"></div>
				</div>
			<div class=\"clrboth\"></div>";
		
		$html .= "<div id=\"tournament-show-div\" rel='{$tid}'>";	
		 
		$html .= $content;	
			 
		$html .= "<div id=\"pagination_top_rank\">".$pagination['links']."</div> 
			</div></div>";
			
		return $html;
	}
	
	/**
	 * Usage:
	 * {pyro:sys:slideshow}
	 */
	function slideshow()
	{
		$ci = & get_instance();
		$html = "";
		$task = $this->uri->segment(3,'');
		if( ($ci->method == 'index' OR $task == 'live_tournament ') AND $ci->controller == 'cricket'){
			$picture = $ci->db->select("FILE.*")
							->from("files AS FILE")
							->join("file_folders AS FOLDER","FILE.folder_id=FOLDER.id")
							->where("FOLDER.slug",'slideshow')
							->get()
							->result();
			//".$this->_getLatestNew()."			
			$html .= "<div id=\"feature\"> 
						<div id=\"latest-new\">".$this->_getHomepageText()."</div>
						<div id=\"slideshow\">	<div class=\"slideshow\">
						";
			
			foreach($picture as $object){
				if($ci->phpvalidator->is_url($object->description)){
					$html .= "<a href='".$object->description."'>".
					"<img src='".thumb2("./uploads/default/files/".$object->filename, 690, 250)."' />"."</a>";
				}else{
					$html .= "<img src='".thumb2("./uploads/default/files/".$object->filename, 690, 250)."' />";
				}
			}
			$html .= "</div></div><div class=\"left_arrow\"></div>
						<div class=\"right_arrow\"></div>
						</div>";
						
			$html .= "<script type=\"text/javascript\">
					google.setOnLoadCallback(function() {
						$(\"#latest-new ul li\").bind(\"click\",function(){
							var link = $(this).find(\"a\").attr(\"href\");
							queryURL(link);
						});
					});
				</script>";	
					
		}							
		return $html;
	}
	
	function displayBanner($pos){
		$html = "";
		$ci = & get_instance();
		$left_array_obj = $ci->advertising_entity->getListAdvByPosition($pos);
		foreach($left_array_obj as $item){
			$ADV = $ci->advertising_entity->init('id',$item->id);
			$w = ($pos == 'left') ? LEFT_BANNER_W:RIGHT_BANNER_W ;
			if($ADV->getUrl()){
				$html .= "<a href='".site_url('cricket/contentload/advertising?id='.$item->id)."' target='_blank'>".thumb($ADV->getAdvImage(),$w)."</a>";
			}else{
				$html .= thumb($ADV->getAdvImage(),$w);
			}
			$html .= '<div class="clrboth"></div>';
		}
		return $html;
	}
	
	function left(){
		return $this->displayBanner('left');
	}
	
	function homebanner(){
		return $this->displayBanner('home');
	}
	
	function right(){
		$ci = & get_instance();
		if(isUserLogin()){
			$USER = $ci->user_entity->init('id', getUserId());
		}
		$_GET['tid'] = isset($_GET['tid']) ? intval($_GET['tid']) : 0;
		$_GET['mid'] = isset($_GET['mid']) ? intval($_GET['mid']) : 0;
		$_GET["scrid"] = isset($_GET["scrid"]) ? $_GET["scrid"] : "";
		$bet_action = site_url('cricket/async/placebet');
		$convert_action = site_url('cricket/async/convert');
		$add_coins = site_url('blog/view/how-to-get-cxcoins'); //cricket/account/add_coins
		
		$TOURNAMENT = $ci->tournament_entity->init('id',$_GET['tid']);
		$MATCH = $ci->match_entity->init('id',$_GET['mid']);
		$rate_convertion = rateConvertion(($_GET['mid']));
		$rate_parse = explode(':',$rate_convertion);
		$pointRate = $rate_parse[0];
		$coinRate = $rate_parse[1];
			
		$html  = "";
		
		if($TOURNAMENT){
			$endTime = $TOURNAMENT->getEndTime() - time();
			$html .= "
				<div class=\"block\" id=\"sys_plugin_tournament_countdown_time\">
					<div class=\"clock-wrap\">
						<div class=\"wrap\">
							<div class=\"title-clock\">Tournament Ends In:</div>
							<div class=\"clrboth\"></div>
							<div class=\"wrap-div-clock\">
								<span class=\"brown countdown upper\" id=\"countdown_t_{$TOURNAMENT->getId()}\" rel=\"{$endTime}\"></span>
							</div>
						</div>
					</div>
				</div>
				<div class=\"clrboth\"></div>
			";
		}
		
		if(isUserLogin()){
			
			$object_result = $ci->user_tournament_entity->findUserInTournament($USER->getId(), ($_GET['tid']));
			if($object_result){
				$point = bet_format( $object_result->point );
				$rank = $ci->user_tournament_entity->myRankInTournament($USER->getId(), ($_GET['tid']));
			}else{
				$point = 0;
				$rank ='';
			}
			
			if($USER->getAvatar()){
				$avatar = thumb($USER->getMyAvatar(),63,60);
			}else{
				$avatar = "<img src='".site_url()."addons/default/themes/clicxa/css/images/avatar.jpg"."' />";
			}
			
			$totalCoinsStaked = $ci->user_tournament_entity->myCoinsStakedInTournament($USER->getId(), $_GET['tid']);
			$limitCoinsStaked = getLimitCoinStake($_GET['tid']);
			
			$coinsLeft = ($limitCoinsStaked == 0) ? 'Unlimitted' : ($limitCoinsStaked - $totalCoinsStaked); 
			if($coinsLeft !== 'Unlimitted'){
				$coinsLeft = ($coinsLeft<= 0) ? 0:$coinsLeft;
			}
		 	$cximage = loadCX();
			
			$html .= "<div class=\"block  hidden\" id=\"sys_plugin_my_profile\">
					<div id=\"avatar\">
						{$avatar}
						<div class=\"clrboth\"></div>
						<span class=\"bold\" style=\"font-weight:bold;color:#CC3300;\">{$USER->getUserName()}</span>
					</div>
					<div class=\"avatar\" style=\"margin-top:0px;margin-bottom:0px;\">
						<span class=\"normal\" style=\"float:left;margin-top:0px;margin-left:5px;line-height:24px;padding-right:2px;font-weight:bold;color:#006699;\">
							<font color=\"#CC3300;\">Points scored:</font> {$point}
							<div class=\"clrboth\"></div>
							<font color=\"#CC3300;\">Rank:</font> <span style=\"font-size:15px;\">{$rank}</span>
						</span>
					</div>
					<div class=\"avatar\" style=\"border-bottom:0 none;margin-top:0px;margin-bottom:0px;height:60px;\">
						<span class=\"normal\" style=\"float:left;margin-top:5px;margin-left:5px;line-height:15px;font-weight:bold;color:#006699;\">
							<font color=\"#CC3300;\">Left Coin Limit in the <br/>Tournament:</font> 
							<span style=\"font-size:15px;\">{$coinsLeft}</span>
						</span>
					</div>
				</div>
				<div class=\"clrboth\"></div>";
		
			$html .= "<div class=\"title\" style=\"margin-bottom:5px;\" id=\"div_addcoins\">
						<a href=\"{$add_coins}\"><span id=\"addcoins\"></span>
						<h3 class=\"left\" style=\"margin-left:10px;\">Get More Free {$cximage} <span class=\"yl_text\">Coins</span></h3></a>
					</div><div class=\"clrboth\"></div>";	
		/*			
			$html .= "<div id=\"sys_plugin_bet\" class=\"hidden\"><div class=\"title2\"><h3>Prediction Slip</h3></div>
						<div class=\"block\">
						<div class=\"item\">Odds</div>
						<div class=\"clrboth\"></div>
						<form action=\"{$bet_action}\" name=\"placebet_submit\" id=\"placebet_submit\" method=\"post\" enctype=\"multipart/form-data\" >
							<div class=\"item\" id=\"bet\"><span id=\"title_sys_plugin\">To select a Predict click on any price</span> </div>
							<input type=\"hidden\" name=\"tournament_id\" value=\"{$_GET["tid"]}\" />
							<input type=\"hidden\" name=\"match_id\" value=\"{$_GET["mid"]}\" />
							<input type=\"hidden\" name=\"scrid\" value=\"{$_GET["scrid"]}\" />
						
							<div class=\"clrboth\"></div>
							<div class=\"item\" id=\"submit_bet\">
							<a href=\"javascript:void(0);\" id=\"remove_bet\">Remove All</a>
							<div id=\"bet_submit\"><input type=\"submit\" value=\"\" /></div>
						</form>
					</div>
					</div><div class=\"clrboth\"></div></div>";	
		*/			
			if($MATCH AND $MATCH->getEnableConvertion() == 'yes'){	
				$html .= "<div id=\"sys_plugin_convertion\" class=\"hidden\"><div class=\"title2\"><h3>Convertion</h3></div>
							<div class=\"block\">
							<div class=\"item\">
							<form action=\"{$convert_action}\" name=\"convert_submit\" id=\"convert_submit\" method=\"post\" enctype=\"multipart/form-data\" >
								<input name=\"point\" id=\"point\" value=\"Points\" class=\"point2coint\"/> &nbsp; To &nbsp;
								<input name=\"coin\" id=\"coin\" value=\"CX coins\" class=\"point2coint\" disabled=\"disabled\" />
								<input type=\"hidden\" name=\"tournament_id\" value=\"{$_GET["tid"]}\" />
								<input type=\"hidden\" name=\"match_id\" value=\"{$_GET["mid"]}\" />
								<input type=\"hidden\" name=\"scrid\" value=\"{$_GET["scrid"]}\" />
							</form>
							<div class=\"clrboth\"></div>
							Rate: <span id=\"rate_status\">{$rate_convertion}</span>
							<div class=\"clrboth\"></div>
							<div id=\"_submit\"></div>
							</div>
							</div><div class=\"clrboth\"></div></div>";
				$html .= "<script type=\"text/javascript\">
							sysconvertion = {point:{$pointRate},coin:{$coinRate}};
						</script>";
			}		
		}else{
		/*
			$html .= "<div id=\"sys_plugin_bet\" class=\"hidden\"><div class=\"title2\"><h3>Prediction Slip</h3></div>
						<div class=\"block\">
						<div class=\"item\">Odds</div>
						<div class=\"clrboth\"></div>
						<form action=\"{$bet_action}\" name=\"placebet_submit\" id=\"placebet_submit\" method=\"post\" enctype=\"multipart/form-data\" >
							<div class=\"item\" id=\"bet\"><span id=\"title_sys_plugin\">To select a Predict click on any price</span> </div>
							<input type=\"hidden\" name=\"tournament_id\" value=\"{$_GET["tid"]}\" />
							<input type=\"hidden\" name=\"match_id\" value=\"{$_GET["mid"]}\" />
							<input type=\"hidden\" name=\"scrid\" value=\"{$_GET["scrid"]}\" />
						</form>
						<div class=\"clrboth\"></div>
						<div class=\"item\" id=\"submit_bet\">
						<a href=\"javascript:void(0);\" id=\"remove_bet\">Remove All</a>
						<div id=\"bet_submit\"><input type=\"submit\" value=\"\" /></div>
					</div>
					</div><div class=\"clrboth\"></div></div>";	
			*/		
			if($MATCH AND $MATCH->getEnableConvertion() == 'yes'){	
				$html .= "<div id=\"sys_plugin_convertion\" class=\"hidden\"><div class=\"title2\"><h3>Convertion</h3></div>
							<div class=\"block\">
							<div class=\"item\">
							<form action=\"{$convert_action}\" name=\"convert_submit\" id=\"convert_submit\" method=\"post\" enctype=\"multipart/form-data\" >
								<input name=\"point\" id=\"point\" value=\"Points\" class=\"point2coint\"/> &nbsp; To &nbsp;
								<input name=\"coin\" id=\"coin\" value=\"CX coins\" class=\"point2coint\" disabled=\"disabled\" />
								<input type=\"hidden\" name=\"tournament_id\" value=\"{$_GET["tid"]}\" />
								<input type=\"hidden\" name=\"match_id\" value=\"{$_GET["mid"]}\" />
								<input type=\"hidden\" name=\"scrid\" value=\"{$_GET["scrid"]}\" />
							</form>
							<div class=\"clrboth\"></div>
							Rate: <span id=\"rate_status\">{$rate_convertion}</span>
							<div class=\"clrboth\"></div>
							<div id=\"_submit\"></div>
							</div>
							</div><div class=\"clrboth\"></div></div>";
				$html .= "<script type=\"text/javascript\">
							sysconvertion = {point:{$pointRate},coin:{$coinRate}};
						</script>";
			}				
		}		
		
		if($TOURNAMENT){
			$html .= $this->_tournament_prize($TOURNAMENT->getId());
		}
		
		$html .= $this->displayBanner('right');
		
		$task = $this->uri->segment(3,'');
		if( $ci->module == 'cricket' AND ($ci->method == 'index' OR $task == 'live_tournament' ) ){return '';}
		
		$fb = '<div style="background:#fff;margin-bottom:5px;padding:5px;"><div class="fb-like" 
		data-href="http://www.facebook.com/ClicxaCricketPredictionGame" 
		data-send="false" data-width="300" data-show-faces="false" colorscheme="light" ></div></div>';
		
		return "<div id=\"right\">".$fb.$html."</div>";
	}
	
	function top(){
		if(!isUserLogin()){
			$loadingPic = loader_image("id='loading_image_signin' class='hidden'");
			$html =	"<div id=\"header-user1\"><a class=\"bold\" href=\"javascript:void(0);\" id=\"_signin\">Sign in</a> | <a href=\"javascript:void(0);\" id=\"_register\" class=\"ui-corner-all\">Register Now</a></div>
				<div id=\"header_login\">
					<div id=\"closed\"></div>
					
					<div class=\"clrboth\"></div>
					
					<div id=\"label1\">Username:</div>
					<input type=\"text\" class=\"input1\" maxlength=\"100\" id=\"username\" />
					
					<div class=\"clrboth\"></div>
					
					<div id=\"label2\">Password:</div>
					<input type=\"password\" class=\"input2\" maxlength=\"100\" id=\"password\" />
					
					<div class=\"clrboth\"></div>
					
					<div id=\"forgot_details\"><a href=\"javascript:void(0);\">Forgot details</a></div>
					
					<div class=\"clrboth\"></div>
					
					<div id=\"submit_login\"></div> {$loadingPic}
				</div>
				
				<div class=\"clrboth\"></div>
				";
		}else{
			$ci = & get_instance();
			
			if(isUserLogin()){
				$USER = $ci->user_entity->init('id',getUserId());
				if($USER->getStatus() !='activated'){
					//unset($_SESSION['user']);
					redirect(site_url());
				}
			}
	
			$currency_arr = currencyDataConfig();
			
			if(isset($_GET['c'])){
				if(in_array($_GET['c'],array_keys($currency_arr)) AND $_GET['c'] !== $USER->getCurrencyCode()){
					$USER->setCash(changeValueOfMoney($USER->getCash(),$newCurrency = $_GET['c'], $oldCurrency = $USER->getCurrencyCode()));
					$USER->setCurrencyCode($_GET['c']);
					$USER->update();
				}
			}
			
			$currency_list = "<ul>";
			foreach($currency_arr as $key=>$val){
				$currentLink = ($key == $USER->getCurrencyCode()) ? "class='current'":"class='hidden'";
				if($key != 'usd'){
					$CURRENCY = $ci->currency_entity->init('code',$key);
					if($CURRENCY){
						$flag = thumb($CURRENCY->getFileFlag(),20,10);
					}
				}else{
					$flag = thumb($ci->currency_entity->getUSDFlag(),20,10);
				}
				$currency_list .= "<li {$currentLink}><span>{$flag}</span><a href='".site_url().'?c='.$key."' >{$val}</a></li>";
			}
			$currency_list .= "</ul>";
		/**	$currency_list = "<select onchange='changeCurrency(this.value);'>";
			foreach($currency_arr as $key=>$val){
				$current = ($key == $USER->getCurrencyCode()) ? "selected='selected'":"";
				if($key != 'usd'){
					$CURRENCY = $ci->currency_entity->init('code',$key);
					$flag = thumb($CURRENCY->getFileFlag(),20,10);
				}else{
					$flag = thumb($ci->currency_entity->getUSDFlag(),20,10);
				}
				 
				$currency_list .= "<option value='{$key}' {$current}>{$flag}&nbsp;&nbsp;{$val}</option>";
			}
			$currency_list .= "</select>";
			
			$currency_list .= "<script type='text/javascript'>
									function changeCurrency(value){
										queryURL(BASE_URI+'?c='+value);
									}
								</script>";
		**/	
		//<div id=\"currency\">{$currency_list}</div>
			$cximage = loadCX();
			$historyLink = site_url("cricket/account/predict_history");
			$findfr = 'http://www.clicxa.com/members/findFriends.php';
			$html =	"
				<div id=\"header-user\"><span class=\"bold\" >".$USER->getUserName()."</span> | <a href=\"javascript:void(0);\" id=\"_logout\">Logout</a></div>
				<div id=\"history\">
					<span class=\"bet_history\">&nbsp;</span>
					<a href=\"{$historyLink}\">Prediction History</a>
				</div>
				<div class=\"clrboth\"></div>
				<div id=\"header_info\">
					<span><span class=\"findfr\">&nbsp;</span><a href=\"{$findfr}\" target='_blank'>Find Friends</a></span>
				<!--	<span><span class=\"cash\">&nbsp;</span>Cash: ".currencyExchangeScore($USER->getCash(),$USER->getCurrencyCode()).' '.$currency_arr[$USER->getCurrencyCode()]."</span>
					-->
					<span class=\"coin-balance\"><span class=\"coin\">&nbsp;</span>{$cximage} <span class=\"yl_text\">Coin Balance</span>: ".$USER->getCoin()."</span>
					<span><span class=\"bonus\">&nbsp;</span>Bonus: ".$USER->getBonus()."</span>
				</div>";
		}
		/**
		    <span>
				<span class=\"currency\">&nbsp;</span>
				<li><a href=\"#\" class=\"currency\">Change Currency</a>
					<ul>
						{$currency_list}
					</ul>
				</li>
			</span>
		**/			
		return $html;
	}
	
	function check(){
		$login = '0';
		if(isUserLogin()){
			$login = '1';
		}
		
		$html = "<script type=\"text/javascript\">
					var LOGIN = '{$login}';
					
					if(LOGIN == '0'){
						$('#main_nav .dashboard').hide();
						//$('#main_nav .live-tournament').addClass('first');
					}
					
				</script>";
		
		return $html;
	}
	
	function checkAuth(){
		$html ='';
		if(isset($_SESSION['autoLogout'])){
			foreach($_SESSION['autoLogout'] as $link){
				$html .= "<iframe src='$link' width='0' height='0'>
						  </iframe>";
			}
			unset($_SESSION['autoLogout']);
			unset($_SESSION['autoLogin']);
		}	
		
		if(isset($_SESSION['autoLogin'])){
		
			$html = "<script type='text/javascript'>
			/*	my_window = window.open('".$_SESSION['autoLogin'][0]."',
						'mywindow','status=0,width=1,height=1');
				window.setTimeout(function(){my_window.close();},1000);
				*/
				$.ajax({
					 url:'".$_SESSION['autoLogin'][0]."',
					 dataType: 'jsonp'
				});
			</script>";
			$html .= "<iframe src='".$_SESSION['autoLogin'][1]."' width='0' height='0'></iframe>";
			$html .= "<iframe src='".$_SESSION['autoLogin'][1]."' width='0' height='0'></iframe>";
			$html .= "<iframe src='".$_SESSION['autoLogin'][0]."' width='0' height='0'></iframe>";
			
			//unset($_SESSION['autoLogin']);
			unset($_SESSION['autoLogout']);
		}	
	
		return $html;
	}
	
	
	function chatbar(){
		$ci = & get_instance();
		if(isUserLogin()){
			$USER = $ci->user_entity->init('id',getUserId());
		}
		
		$html = '';
		 
		$task = $this->uri->segment(3,'');
		if(isUserLogin() ){
			//print_r($_SESSION['user']);
			//$contents = file_get_contents('http://www.clicxa.com/members/cricket_if.php'); 
			/*	$html .= "<style>#page-wrap{margin-bottom:40px;margin-top:-40px;}</style>";
				$html .= "<div id='charbarBottomsite'>";
				$html .= "<iframe id='chatIFclx' name='chatIFclx' style='height:0px;' width='100%' hidefocus='true' align='bottom' src='http://www.clicxa.com/members/cricket_if.php' border='0'></iframe>";
				$html .= "</div>";
				$html .= "<input type='hidden' name='clxUsername' id='clxUsername' value='{$USER->getUserName()}'>";
			*/
			$html .= "<a href='http://www.clicxa.com/members/cricket_ifr.php' id='clxchatpopup'>Chat</a>";
		}
		 
		return $html;
	}
	
	
	
	
	//end
}

